package com.csaba.swing.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import com.csaba.swing.MainWindow;
import com.csaba.swing.gui.util.DynamicColumnLayout;
import com.csaba.util.LoggerHelper;
import com.csaba.util.encryption.EncryptedStore;

@SuppressWarnings( "serial" )
public final class ChangePasswordDialog extends JDialog
{
	private final static Logger logger = LoggerHelper.getDefaultLogger();

	private JPasswordField oldPassword;
	private JPasswordField password;
	private JPasswordField confirmPassword;
	private final JButton ok;

	public ChangePasswordDialog( final Frame frame )
	{
		super(frame);

		setModal(true);
		setTitle(MainWindow.getString("ChangePasswordDialog.title")); //$NON-NLS-1$
		setResizable(false);

		final AbstractAction okAction = new AbstractAction(MainWindow.getString("ChangePasswordDialog.button.ok")) //$NON-NLS-1$
		{
			@Override
			public void actionPerformed( final ActionEvent arg0 )
			{
				setPassword();
			}
		};

		//create input fields
		final JPanel inputs = new JPanel(new DynamicColumnLayout(new int[] { DynamicColumnLayout.LEFT,
				DynamicColumnLayout.LEFT }));

		inputs.setBorder(BorderFactory.createEmptyBorder());
		inputs.add(new JLabel(MainWindow.getString("ChangePasswordDialog.currentPassword"))); //$NON-NLS-1$
		inputs.add(oldPassword = new JPasswordField(20));
		inputs.add(new JLabel(MainWindow.getString("ChangePasswordDialog.newPassword"))); //$NON-NLS-1$
		inputs.add(password = new JPasswordField(20));
		inputs.add(new JLabel(MainWindow.getString("ChangePasswordDialog.confirmPassword"))); //$NON-NLS-1$
		inputs.add(confirmPassword = new JPasswordField(20));

		oldPassword.addActionListener(okAction);
		password.addActionListener(okAction);
		confirmPassword.addActionListener(okAction);

		//create buttons
		final JPanel buttons = new JPanel(new FlowLayout());
		ok = new JButton(okAction);
		buttons.add(ok);

		final JButton cancel = new JButton(new AbstractAction(MainWindow
				.getString("ChangePasswordDialog.button.cancel")) //$NON-NLS-1$
				{
					@Override
					public void actionPerformed( final ActionEvent e )
					{
						dispose();
					}

				});
		buttons.add(cancel);

		//create content
		final JPanel content = (JPanel) getContentPane();
		content.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		content.setLayout(new BorderLayout());
		content.add(inputs, BorderLayout.CENTER);
		content.add(buttons, BorderLayout.SOUTH);

		pack();
		setLocationRelativeTo(frame);
	}

	private void setPassword()
	{
		final char[] oldPassword = this.oldPassword.getPassword();
		final char[] password1 = password.getPassword();
		final char[] password2 = confirmPassword.getPassword();

		if ( password1.length < 4 )
		{
			JOptionPane
					.showMessageDialog(
							MainWindow.getFrame(),
							MainWindow.getString("ChangePasswordDialog.msg.minPasswordLength"), MainWindow.getString("ChangePasswordDialog.title.error"), //$NON-NLS-1$ //$NON-NLS-2$
							JOptionPane.ERROR_MESSAGE);
			return;
		}

		if ( !InitStoreDialog.equals(password1, password2) )
		{
			JOptionPane.showMessageDialog(MainWindow.getFrame(), MainWindow
					.getString("ChangePasswordDialog.msg.samePassword"), //$NON-NLS-1$
					MainWindow.getString("ChangePasswordDialog.title.error"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$
			return;
		}

		try
		{
			EncryptedStore.getInstance().changePassword(oldPassword, password1);
		}
		catch ( final Exception ex )
		{
			logger.log(Level.SEVERE, "Error changing password in the EncrpyptionStore. ", ex); //$NON-NLS-1$
			JOptionPane.showMessageDialog(MainWindow.getFrame(), MainWindow.getString(
					"ChangePasswordDialog.msg.generalError", ex.getMessage()), //$NON-NLS-1$ 
					MainWindow.getString("ChangePasswordDialog.title.error"), JOptionPane.ERROR_MESSAGE);//$NON-NLS-1$ 
			return;
		}

		dispose();
		EncryptedStore.clear(oldPassword);
		EncryptedStore.clear(password1);
		EncryptedStore.clear(password2);
	}
}
